package com.nowcoder.community.config;

import com.nowcoder.community.quartz.AlphaJob;
import com.nowcoder.community.quartz.PostScoreRefresfJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * @author cjh
 * @date 2023/2/10 - 16:19
 */
@Configuration
//配置--数据库--调用
public class QuartzConfig {

//    factorybean 可以简化bean得实例化过程

    /**
     * 1.通过factorybean 封装了bean得实例化过程
     * 2.将factorybean装配到spring容器中
     * 3.将factorybean 注入到其他容器中
     * 4.该bean得到的是factotybean所管理的对象实例
     *
     * @return
     */
//    @Bean
//    配置jobdetail
    public JobDetailFactoryBean alphaJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(AlphaJob.class);
        factoryBean.setName("alphaJob");
        factoryBean.setGroup("alphaJobGroup");
        factoryBean.setDurability(true);
//        是否可恢复
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }

    //    @Bean
//    需要上面得作为参数  保持同名
//    配置trigger  ---SimpleTriggerFactoryBean/CronTriggerFactoryBean
    public SimpleTriggerFactoryBean alphaTrigger(JobDetail alphaJobDetail) {

        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(alphaJobDetail);
        factoryBean.setName("alphaTrigger");
        factoryBean.setGroup("alphaTrigger");
        factoryBean.setRepeatInterval(3000);
        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }


    @Bean
//    刷新帖子分数任务
    public JobDetailFactoryBean postScoreRefreshJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(PostScoreRefresfJob.class);
        factoryBean.setName("postScoreRefreshJob");
        factoryBean.setGroup("communityJobGroup");
        factoryBean.setDurability(true);
//        是否可恢复
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }

    @Bean
//    需要上面得作为参数  保持同名
//    配置trigger  ---SimpleTriggerFactoryBean/CronTriggerFactoryBean
    public SimpleTriggerFactoryBean postScoreRefreshTrigger(JobDetail postScoreRefreshJobDetail) {

        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(postScoreRefreshJobDetail);
        factoryBean.setName("postScoreRefreshTrigger");
        factoryBean.setGroup("communityTriggerGroup");
//        factoryBean.setRepeatInterval(1000 *60 *5);
        factoryBean.setRepeatInterval(1000 * 60);

        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }
}
